<template>
  <page-header-wrapper>
    <template v-slot:content>
      <div class="page-header-content">
        <div class="avatar">
          <a-avatar size="large" :src="user.avatar" />
        </div>
        <div class="content">
          <div class="content-title">
            {{ timeFix }}，{{ user.username }}<span class="welcome-text">，欢迎回来</span>
          </div>
          <div>{{user.org}}</div>
        </div>
      </div>
    </template>
    <template v-slot:extraContent>
      <div class="extra-content">
        <div class="stat-item">
          <a-statistic title="我创建的报表" :value="overview.created" />
        </div>
        <div class="stat-item">
          <a-statistic title="我分享的报表" :value="overview.sharedTo" />
        </div>
        <div class="stat-item">
          <a-statistic title="分享给我的报表" :value="overview.sharedFrom" />
        </div>
      </div>
    </template>

    <div>
      <a-row :gutter="24">
        <a-col :xl="16" :lg="24" :md="24" :sm="24" :xs="24">
          <a-card
            class="project-list"
            :loading="loading.report"
            style="margin-bottom: 24px;"
            :bordered="false"
            title="关注的报表"
            :body-style="{ padding: 0 }"
          >
            <a slot="extra" v-on:click="toReportList">全部报表</a>
            <div>
              <a-card-grid class="project-card-grid" :key="i" v-for="(item, i) in reportList">
                <a-card :bordered="false" :body-style="{ padding: 0 }">
                  <a-card-meta>
                    <div slot="title" class="card-title">
                      <a-avatar size="small" :src="reportIcon" />
                      <a v-on:click="toReport(item.reportId)">{{ item.name }}</a>
                    </div>
                    <div slot="description" class="card-description">
                      {{ item.remark }}
                    </div>
                  </a-card-meta>
                  <div class="project-item">
                    <a>{{item.creatorName}}</a>
                    <span class="datetime">{{item.updatedAt}}</span>
                  </div>
                </a-card>
              </a-card-grid>
            </div>
          </a-card>

          <a-card title="应用" :bordered="false" style="margin-bottom: 24px;">
            <div>
              <a-card-grid class="project-card-grid" :key="i" v-for="(item, i) in appList">
                <a-card :bordered="false" :body-style="{ padding: 0 }">
                  <a-card-meta>
                    <div slot="title" class="card-title">
                      <!--                      <a-avatar size="small" :src="item.cover" />-->
                      <a v-on:click="toReport(item.reportId)">{{ item.name }}</a>
                    </div>
                    <div slot="description" class="card-description">
                      {{ item.remark }}
                    </div>
                  </a-card-meta>
                </a-card>
              </a-card-grid>
            </div>
          </a-card>
        </a-col>

        <a-col
          style="padding: 0 12px"
          :xl="8"
          :lg="24"
          :md="24"
          :sm="24"
          :xs="24">
          <a-card :loading="loading.announcement" title="系统公告" :bordered="false">
            <a-list>
              <a-list-item :key="index" v-for="(item, index) in announcementList">
                <a-list-item-meta>
                  <div slot="title">
                    <span>{{ item.creatorName }}</span>&nbsp;在&nbsp;<span>{{ item.createdAt }}</span>&nbsp;发布了系统公告&nbsp;<a v-on:click="showAnnouncementContent(item.title, item.content)">{{ item.title }}</a>
                  </div>
                  <div slot="description">{{ item.summary }}</div>
                </a-list-item-meta>
              </a-list-item>
            </a-list>
          </a-card>
        </a-col>
      </a-row>
    </div>
  </page-header-wrapper>
</template>

<script>
import { timeFix } from '@/utils/util'
import { PageHeaderWrapper } from '@ant-design-vue/pro-layout'
import { Radar } from '@/components'
import store from "@/store";
import {mapActions} from "vuex";

export default {
  name: 'Workplace',
  components: {
    PageHeaderWrapper,
    Radar
  },
  data () {
    return {
      loading: {
        overview: false,
        report: false,
        announcement: false,
      },
      timeFix: timeFix(),
      avatar: '',
      user: {
        avatar: require('@/assets/BiazfanxmamNRoxxVxka.png')
      },
      reportIcon: require('@/assets/icons/report.jpeg'),
      overview: {
        created: "---",
        sharedTo: "---",
        sharedFrom: "---",
      },
      reportList: [],
      appList: [],
      announcementList: [],

    }
  },
  created () {
    this.user.username = store.getters.userInfo.username
    let orgs = store.getters.orgs
    let organizations = []
    if (orgs && orgs.length > 0){
      for (let i in orgs){
        organizations.push(orgs[i].name)
      }
    }
    this.user.org = organizations.join(",")
  },
  mounted () {
    this.getOverviewData()
    this.getFollowingReportData()
    this.getAnnouncementData()
  },
  methods: {
    ...mapActions('announcementManage', ['getAnnouncements']),
    ...mapActions('reportManage', ['getFollowingReports']),
    getOverviewData (){
      store.dispatch("getUserOverviewData").then(res => {
        this.overview = res
      }).catch(() => {
        this.overview = {
          created: "---",
          sharedTo: "---",
          sharedFrom: "---",
        }
      })
    },
    toReportList (){
      this.$router.push({name:"ReportList"})
    },
    toReport(reportId) {
      this.$router.push({path: "/system/show_reports/" + reportId, params: {id: reportId}})
    },
    getFollowingReportData () {
      this.loading.report = true
      this.getFollowingReports().then(res => {
        this.reportList = res
      }).finally(()=>{
        this.loading.report = false
      })
    },
    getAnnouncementData () {
      this.loading.announcement = true
      const params = {
        page: 1,
        pageSize: 10,
        order:'desc',
        orderBy: 'createdAt'
      }
      this.getAnnouncements(params).then(res => {
        let data = res
        let summary = ""
        data.map(item => {
          if (typeof item.content === 'string' && item.content.length > 255){
            summary = item.content.substring(0,255) + "……"
          }else {
            summary = item.content
          }
          item.summary = summary
        })
        this.announcementList = data
      }).finally(() =>{
        this.loading.announcement = false
      })
    },
    showAnnouncementContent (title,content) {
      const h = this.$createElement;
      this.$info({
        title: title,
        content: h('div', {class:"text-wrapper"}, [
          h('p', content),
        ]),
        onOk() {},
      });
    }
  }
}
</script>

<style lang="less" scoped>
@import './Workplace.less';

.project-list {
  .card-title {
    font-size: 0;

    a {
      color: rgba(0, 0, 0, 0.85);
      margin-left: 12px;
      line-height: 24px;
      height: 24px;
      display: inline-block;
      vertical-align: top;
      font-size: 14px;

      &:hover {
        color: #1890ff;
      }
    }
  }

  .card-description {
    color: rgba(0, 0, 0, 0.45);
    height: 44px;
    line-height: 22px;
    overflow: hidden;
  }

  .project-item {
    display: flex;
    margin-top: 8px;
    overflow: hidden;
    font-size: 12px;
    height: 20px;
    line-height: 20px;

    a {
      color: rgba(0, 0, 0, 0.45);
      display: inline-block;
      flex: 1 1 0;

      &:hover {
        color: #1890ff;
      }
    }

    .datetime {
      color: rgba(0, 0, 0, 0.25);
      flex: 0 0 auto;
      float: right;
    }
  }

  .ant-card-meta-description {
    color: rgba(0, 0, 0, 0.45);
    height: 44px;
    line-height: 22px;
    overflow: hidden;
  }
}

.item-group {
  padding: 20px 0 8px 24px;
  font-size: 0;

  a {
    color: rgba(0, 0, 0, 0.65);
    display: inline-block;
    font-size: 14px;
    margin-bottom: 13px;
    width: 25%;
  }
}

.members {
  a {
    display: block;
    margin: 12px 0;
    line-height: 24px;
    height: 24px;

    .member {
      font-size: 14px;
      color: rgba(0, 0, 0, 0.65);
      line-height: 24px;
      max-width: 100px;
      vertical-align: top;
      margin-left: 12px;
      transition: all 0.3s;
      display: inline-block;
    }

    &:hover {
      span {
        color: #1890ff;
      }
    }
  }
}

.mobile {
  .project-list {
    .project-card-grid {
      width: 100%;
    }
  }

  .more-info {
    border: 0;
    padding-top: 16px;
    margin: 16px 0 16px;
  }

  .headerContent .title .welcome-text {
    display: none;
  }
}

.text-wrapper {
  white-space: pre-wrap;
}
</style>

<style>
.ant-pro-page-header-wrap-children-content {
  display: block;
}
</style>
